package com.guigu.chenjiajun.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.chenjiajun.service.WorkOrderReviewService;
import com.guigu.chenjiajun.vo.MManufactureVo;
import com.guigu.common.mapper.*;
import com.guigu.common.pojo.*;
import com.guigu.common.util.GetUUidUtil;
import com.guigu.common.vo.SearchVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class WorkOrderReviewServiceImpl implements WorkOrderReviewService {

    @Resource
    private GetUUidUtil getUUidUtil;
    @Resource
    private MManufactureMapper mManufactureMapper;

    @Resource
    private MProcedureMapper mProcedureMapper;

    @Resource
    private SPayMapper sPayMapper;

    @Resource
    private MProcedureModuleMapper mProcedureModuleMapper;

    @Override
    public SearchVo<MManufacture> queryProcedure(SearchVo searchVo) {
        PageHelper.startPage(searchVo.getPageNumber(), searchVo.getPageSize());
        PageInfo p = new PageInfo(this.mManufactureMapper.queryProcedure(searchVo));
        searchVo.setRows(p.getList());
        searchVo.setTotal(p.getTotal());
        return searchVo;
    }

    @Override
    public MManufactureVo viewAudit(String id) {
        MManufactureVo vo = this.mManufactureMapper.viewAudit(id);
        vo.setProcedureList(this.mProcedureMapper.queryProcedure(vo.getId()));
        return vo;
    }

    @Override
    public int DispatchingAudit(MManufacture mManufacture) {
        try {
            this.mManufactureMapper.DispatchingAudit(mManufacture);
            if (mManufacture.getCheckTag().equals("s001-1")) {
                MManufacture mManufacture1 = this.mManufactureMapper.selectByManufactureId(mManufacture.getManufactureId());
                 this.sPayMapper.insertPay(getUUidUtil.returnId("402"), mManufacture1);
                List<MProcedureModule> mProcedureModule = this.mProcedureModuleMapper.selectModule(mManufacture.getManufactureId());
                for (int i = 0; i < mProcedureModule.size(); i++) {
                    MProcedureModule ProcedureModule = mProcedureModule.get(i);
                    ProcedureModule.setParentId(mManufacture1.getId());
                    ProcedureModule.setDetailsNumber(i + 1);
                    this.mManufactureMapper.insertPay_details(ProcedureModule);
                }

            }
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
        return 1;
    }


}
